<?php
Globals::requireClass('Controller');
Globals::requireClass('Alipay');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Product');
Globals::requireTable('Order');
Globals::requireTable('Days');
Globals::requireTable('Pay');
Globals::requireTable('Account');

class PayController extends Controller
{
	protected $UserTable;
	protected $ProductTable;
	protected $OrderTable;
	protected $DaysTable;
	protected $PayTable;
	protected $AccountTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable 	= new UserTable($config);
		$this->ProductTable = new ProductTable($config);
		$this->OrderTable   = new OrderTable($config);
		$this->DaysTable    = new DaysTable($config);
		$this->PayTable     = new PayTable($config);
		$this->AccountTable = new AccountTable($config);
	}
	
	public function indexAction()
	{
		
	}
	
	public function onedayAction()
	{
	    $uid = $this->getParam('uid');
	    $did = $this->getIntParam('dayid');
	    $day = $this->DaysTable->getRow($did);
	    $token = $this->getParam('token');
	    $machine_type = $this->getIntParam('machine_type');
	    
	    if($uid < 1){
	        $usermodel = new UserModel();
	        $uid = $usermodel->getUserIdByToken($token,$machine_type);
	    }
	    
	    if(empty($uid) || empty($did) || $day['status'] == 0){
	        echo json_encode(array('returncode'=>-999,'orderid'=>''));
	        exit();
	    }
	    
	    $time = time();
	    $ispayed = $this->OrderTable->getRow(array('uid'=>$uid,'status'=>1,'pid'=>$day['pid'],'pasttime>?'=>$time));
	    if($ispayed['id'] > 0){
	        echo json_encode(array('returncode'=>-1,'orderid'=>''));
	        exit();
	    }
		
	    $aid = 0;
		$signtime = strtotime(date('Y-m-d'));
		$timevalue = $signtime + (int)$day['days'] * 86400;
	    $accounts = $this->AccountTable->listAll(array('pid'=>$day['pid'],'endtime'=>$timevalue,'`nums` > 0'));
	    foreach ($accounts as $v){
	        if($aid == 0){
	            $countvalue = $this->OrderTable->listCount(array('aid'=>$v['id'],'pasttime>?'=>$time));
	            if($countvalue < $v['nums']){
	                $aid = $v['id'];
	                $username = $v['username'];
	                $password = $v['password'];
	            }
	        }
	    }
	    if($aid == 0){
	        $accounts = $this->AccountTable->getRow(array('pid'=>$day['pid'],'endtime'=>0,'`nums` > 0'));
	        if($accounts['id'] > 0){
	            $aid = $accounts['id'];
	            $username = $accounts['username'];
	            $password = $accounts['password'];
	        }
	    }
	    
	    if($aid < 1){
	        echo json_encode(array('returncode'=>-3,'orderid'=>''));
	        exit();
	    }
	    
	    $data['orderid'] = substr($time, -9) . mt_rand(10, 99);
	    $data['uid'] = $uid;
	    $data['pid'] = $day['pid'];
	    $data['did'] = $day['id'];
	    $data['createtime'] = date('Y-m-d H:i:s');
	    $data['status'] = 0;
	    $this->PayTable->add($data);
	    echo json_encode(array('returncode'=>0,'orderid'=>$data['orderid']));
	    exit();
	}
	
	public function payokAction()
	{
	    $orderid = $this->getParam('orderid');
	    $payorderid = $this->getParam('payorderid');
	    $paytype = $this->getParam('paytype');
	    $sign = $this->getParam('sign');
	    $time = time();
	    
	    $order = $this->PayTable->getRow(array('orderid'=>$orderid));
	    $day = $this->DaysTable->getRow($order['did']);
	    
	    if($order['id'] < 1 || $order['status'] != 0 || $day['id'] < 1){
	        echo json_encode(array('returncode'=>-3,'username'=>'','password'=>''));
	        exit();
	    }
	    
	    $new_num_arr = array();
	    $orderid_num = $orderid + 19921206;
	    for ($ii = 0 ; $ii < strlen($orderid_num) ; $ii++){
	        $tmp_num = substr($orderid_num, $ii , 1);
	        $new_num_arr[] = $tmp_num;
	    }
	    krsort($new_num_arr);
	    $new_num = '';
	    foreach ($new_num_arr as $v){
	        $new_num .= (string)$v;
	    }
	    if($sign != $new_num){
	        echo json_encode(array('returncode'=>-2,'username'=>'','password'=>''));
	        exit();
	    }
        
	    $alipay = new Alipay();
	    $re = $alipay->queryone('',$payorderid);
	    $alipay_money = $re->response->trade->total_fee;
	    if($day['money'] != $alipay_money){
	        echo json_encode(array('returncode'=>-999,'username'=>'','password'=>''));
	        exit();
	    }
	    
	    $signtime = strtotime(date('Y-m-d'));
	    $aid = 0;
	    $timevalue = $signtime + (int)$day['days'] * 86400;
	    $accounts = $this->AccountTable->listAll(array('pid'=>$order['pid'],'endtime'=>$timevalue,'`nums` > 0'));
	    foreach ($accounts as $v){
	        if($aid == 0){
	            $countvalue = $this->OrderTable->listCount(array('aid'=>$v['id'],'pasttime>?'=>$time));
	            if($countvalue < $v['nums']){
	                $aid = $v['id'];
	                $username = $v['username'];
	                $password = $v['password'];
	            }
	        }
	    }
	    if($aid == 0){
	        $accounts = $this->AccountTable->getRow(array('pid'=>$order['pid'],'endtime'=>0,'`nums` > 0'));
	        if($accounts['id'] > 0){
	            $aid = $accounts['id'];
	            $username = $accounts['username'];
	            $password = $accounts['password'];
	            
	            $this->AccountTable->update(array('endtime'=>$timevalue),$accounts['id']);
	        }
	    }
	    
	    if($aid < 1){
	        echo json_encode(array('returncode'=>-3,'username'=>'','password'=>''));
	        exit();
	    }
	    
	    switch ($paytype){
	        case 'alipay':
	            $paytypevalue = 1;
	            break;
	        default:
	            $paytypevalue = 1;
	            break;
	    }
	    
	    $data['status'] = 1;
	    $data['paytime'] = date('Y-m-d H:i:s');
	    $data['paytype'] = $paytypevalue;
	    $data['payorderid'] = $payorderid;
	    $data['aid'] = $aid;
	    $this->PayTable->update($data,$order['id']);
	    unset($data);
	    
	    $data['orderid'] = $orderid;
	    $data['uid'] = $order['uid'];
	    $data['pid'] = $order['pid'];
	    $data['did'] = $order['did'];
	    $data['aid'] = $aid;
	    $data['starttime'] = $time;
	    $data['pasttime'] = $time + 86400 * $day['days'];
	    $data['status'] = 1;
	    $this->OrderTable->add($data);
	    
	    echo json_encode(array('returncode'=>0,'username'=>$username,'password'=>$password));
	    exit();
	}
	
	protected function out()
	{
		parent::out();
	}
}

Config::extend('PayController', 'Controller');
